package com.shanxi.cz.module.biz.service.ammeterrecordmonth;

import com.shanxi.cz.framework.common.pojo.PageResult;
import com.shanxi.cz.framework.common.util.object.BeanUtils;
import com.shanxi.cz.module.biz.controller.admin.ammeterrecordmonth.vo.AmmeterRecordMonthPageReqVO;
import com.shanxi.cz.module.biz.controller.admin.ammeterrecordmonth.vo.AmmeterRecordMonthSaveReqVO;
import com.shanxi.cz.module.biz.dal.dataobject.ammeterrecordmonth.AmmeterRecordMonthDO;
import com.shanxi.cz.module.biz.dal.mysql.ammeterrecordmonth.AmmeterRecordMonthMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.shanxi.cz.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.shanxi.cz.module.biz.enums.ErrorCodeConstants.AMMETER_RECORD_MONTH_NOT_EXISTS;

/**
 * 电表数据日报 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class AmmeterRecordMonthServiceImpl implements AmmeterRecordMonthService {

    @Resource
    private AmmeterRecordMonthMapper ammeterRecordMonthMapper;

    @Override
    public Integer createAmmeterRecordMonth(AmmeterRecordMonthSaveReqVO createReqVO) {
        // 插入
        AmmeterRecordMonthDO ammeterRecordMonth = BeanUtils.toBean(createReqVO, AmmeterRecordMonthDO.class);
        ammeterRecordMonthMapper.insert(ammeterRecordMonth);

        // 返回
        return ammeterRecordMonth.getId();
    }

    @Override
    public void updateAmmeterRecordMonth(AmmeterRecordMonthSaveReqVO updateReqVO) {
        // 校验存在
        validateAmmeterRecordMonthExists(updateReqVO.getId());
        // 更新
        AmmeterRecordMonthDO updateObj = BeanUtils.toBean(updateReqVO, AmmeterRecordMonthDO.class);
        ammeterRecordMonthMapper.updateById(updateObj);
    }

    @Override
    public void deleteAmmeterRecordMonth(Integer id) {
        // 校验存在
        validateAmmeterRecordMonthExists(id);
        // 删除
        ammeterRecordMonthMapper.deleteById(id);
    }

    @Override
    public void deleteAmmeterRecordMonthListByIds(List<Integer> ids) {
        // 删除
        ammeterRecordMonthMapper.deleteByIds(ids);
    }


    private void validateAmmeterRecordMonthExists(Integer id) {
        if (ammeterRecordMonthMapper.selectById(id) == null) {
            throw exception(AMMETER_RECORD_MONTH_NOT_EXISTS);
        }
    }

    @Override
    public AmmeterRecordMonthDO getAmmeterRecordMonth(Integer id) {
        return ammeterRecordMonthMapper.selectById(id);
    }

    @Override
    public AmmeterRecordMonthDO getAmmeterRecordMonth(Integer ammeterId, String recordMonth) {
        return ammeterRecordMonthMapper.selectFirstOne(AmmeterRecordMonthDO::getAmmeterId, ammeterId, AmmeterRecordMonthDO::getRecordMonth, recordMonth);
    }

    @Override
    public PageResult<AmmeterRecordMonthDO> getAmmeterRecordMonthPage(AmmeterRecordMonthPageReqVO pageReqVO) {
        return ammeterRecordMonthMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AmmeterRecordMonthDO> getAmmeterRecordMonthByYear(AmmeterRecordMonthPageReqVO pageRecordMonthReqVO) {
        return ammeterRecordMonthMapper.selectList(pageRecordMonthReqVO);
    }

}